package com.example.bobo.yuejian322.Fragment;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.Toast;


import com.example.bobo.yuejian322.Activity.MainActivity;
import com.example.bobo.yuejian322.Activity.UpdateMyActivity;
import com.example.bobo.yuejian322.Interfa.LoginSeverice;
import com.example.bobo.yuejian322.Interfa.YuejianService;
import com.example.bobo.yuejian322.R;
import com.example.bobo.yuejian322.base.BaseFragment;
import com.example.bobo.yuejian322.bean.GuanyuYuejian;
import com.example.bobo.yuejian322.bean.LoginOut;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class MainFragment extends BaseFragment implements  NavigationView.OnNavigationItemSelectedListener{
  //  private TextView titleView;
    private RadioGroup radioGroup;

    private View view;
    private PeixunFragment peixunFragment;
    private ClubFragment clubFragment;
    private ZhuyeFragment zhuyeFragment;
    private SaituFragment saituFragment;
    private MyFragment myFragment;
    private Menu menu;
    private FragmentManager fragmentManager;
    private LoginFragment loginFragment;

    private FragmentManager manager;
    private FragmentTransaction transaction;

    public MainFragment() {
        // Required empty public constructor
    }

    private void replace(Fragment fragment) {

        transaction = manager.beginTransaction();
        transaction.replace(R.id.fragmentLayout, fragment);
        transaction.commit();


    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
      //  titleView = (TextView) view.findViewById(R.id.textView_titel);
        radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
        manager = getChildFragmentManager();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radio_peixun:
                        peixunFragment = new PeixunFragment();
                        replace(peixunFragment);
                        break;


                    case R.id.radio_club:
                        clubFragment = new ClubFragment();
                        replace(clubFragment);
                        break;


                    case R.id.radio_zhuye:

                        zhuyeFragment = new ZhuyeFragment();
                        replace(zhuyeFragment);

                        break;

                    case R.id.radio_saitu:

                        saituFragment = new SaituFragment();
                        replace(saituFragment);

                        break;



                    case R.id.radio_my:
                        myFragment = new MyFragment();
                        replace(myFragment);


                        break;
                }

            }
        });

        radioGroup.check(R.id.radio_zhuye);
        //onCreateOptionsMenu();


    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

         view=inflater.inflate(R.layout.activity_delete, container, false);
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);


        DrawerLayout drawer = (DrawerLayout) view.findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                getActivity(), drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) view.findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        toolbar.setVisibility(View.GONE);

        return view;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        if (id == R.id.nav_camera) {
         Intent intent = new Intent();
            intent.setClass(getActivity(), UpdateMyActivity.class);
            startActivity(intent);
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {
        } else if (id == R.id.nav_slideshow) {
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            Uri uri1=Uri.parse("package:" + getActivity().getPackageName());
            intent.setData(uri1);
            startActivity(intent);
        } else if (id == R.id.nav_manage) {
        } else if (id == R.id.nav_share) {
            Toast.makeText(getActivity(), "dianji", Toast.LENGTH_SHORT).show();
            zhuxiao();
        }

        DrawerLayout drawer = (DrawerLayout) view.findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    private  void zhuxiao(){

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175/workout/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        LoginSeverice loginSeverice =retrofit.create(LoginSeverice.class);
        Call<LoginOut> listCall = loginSeverice.logout(getUserId());

        listCall.enqueue(new Callback<LoginOut>() {
            @Override
            public void onResponse(Call<LoginOut> call, Response<LoginOut> response) {
                if(getActivity()==null){

                    Toast.makeText(getActivity(), "注销成功", Toast.LENGTH_SHORT).show();


                }
                else {

                    Toast.makeText(getActivity(), "注销失败", Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onFailure(Call<LoginOut> call, Throwable t) {

            }
        });







    }


}
